<!--
 * @Author: alan_mf
 * @Date: 2023-05-12 15:44:57
 * @LastEditors: upp24 1246815351@qq.com
 * @LastEditTime: 2023-07-22 13:01:59
 * @FilePath: /pure-admin-thin-i18n/src/views/order/endOrder/index.vue
 * @Description: 
 * 
-->
<script setup lang="ts">
import { reactive, ref, onMounted } from "vue";
import { adminInfo, adminInfoEdit } from "@/api/settings";
import * as CryptoJS from "crypto-js";
import { message } from "@/utils/message";
import { storageSession } from "@pureadmin/utils";
import { getSessionKey } from "@/utils/auth";
import { useUserStoreHook } from "@/store/modules/user";
const formIs = ref({
  admin_id: "",
  admin_name: "",
  admin_pass: "",
  admin_nick: ""
});
const adminPass = ref();
const roleUser = ref();
const show = ref(false);
const formLabelWidth = "100px";
defineOptions({
  // name 作为一种规范最好必须写上并且和路由的name保持一致
  name: "UpPassword"
});
async function handleUser(roleId) {
  const adminInfo = getSessionKey();
  roleUser.value = adminInfo.admin_id;
  formIs.value = adminInfo;
  show.value = true;
  console.log(res);
}
//提交修改
async function handleSubmit() {
  const role = {
    admin_id: roleUser.value,
    admin_name: formIs.value.admin_name,
    admin_pass: adminPass.value,
    admin_nick: formIs.value.admin_nick
  };
  // 将formIsRef.value对象转换为JSON字符串
  const formIsRefStr = JSON.stringify(role);

  // 对JSON字符串进行Base64加密
  const encryptedFormIsRef = CryptoJS.enc.Base64.stringify(
    CryptoJS.enc.Utf8.parse(formIsRefStr)
  );
  const encryptedFormIsRefValue = {
    value: encryptedFormIsRef
  };
  const res = await adminInfoEdit(encryptedFormIsRefValue);
  if (res.code === 200) {
    const asyncRouteList = storageSession().getItem("user-info") as any;
    if (formIs.value.admin_nick != asyncRouteList.admin_nick) {
      asyncRouteList.admin_nick = formIs.value.admin_nick;
      storageSession().setItem("user-info", asyncRouteList);
      location.reload();
    } else {
      // TODO: 执行登出操作
      useUserStoreHook().logOut();
    }
    message("修改成功", { type: "success" });
  }
}
/** 退出登录 */
function logout() {
  useUserStoreHook().logOut();
  return reactive({
    isLoggedOut: true
  });
}
onMounted(() => {
  const roleId = getSessionKey().role_id;
  handleUser(roleId);
});
</script>

<template>
  <div id="mian" v-if="show">
    <el-form>
      <el-form-item label="管理员账户" :label-width="formLabelWidth">
        <el-input
          size="large"
          v-model="formIs.admin_name"
          autocomplete="off"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="管理员密码" :label-width="formLabelWidth">
        <el-input
          size="large"
          type="password"
          v-model="adminPass"
          autocomplete="off"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="管理员姓名" :label-width="formLabelWidth">
        <el-input
          size="large"
          v-model="formIs.admin_nick"
          autocomplete="off"
          style="width: 150px"
        />
      </el-form-item>
    </el-form>
    <span>
      <el-button type="primary" @click="handleSubmit()" v-auth="'pass_sumbit'">
        提交
      </el-button>
    </span>
  </div>
</template>
